package vitaliy.bondar.diamondcave;

import org.andengine.engine.camera.Camera;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.graphics.Point;

public abstract class Player extends MovingGameObject implements LiveGameObject
{
	private long[] animationDurations;
	private int[] animationLeft;
	private int[] animationRight;
	private int[] animationUp;
	private int[] animationDown;
    public Player( GameScene scene, final int pX, final int pY, VertexBufferObjectManager vbo, Camera camera )
    {
        super( scene, GameObject.Type.PLAYER, pX, pY, ResourcesManager.getInstance().player_region, vbo);
        camera.setChaseEntity(this);
        m_moveDuration = 0.15f;
        animationDurations = new long[] {25,25,25};
        animationLeft = new int[] {9,11,10};
        animationRight = new int[] {3,5,4};
        animationUp = new int[] {6,8,7};
        animationDown = new int[] {0,2,1};
    }
    
    public abstract void onDie();
    
    @Override
    public void move( Direction direction ) 
    {
    	switch( direction ) {
    	case RIGHT:
    	{
    		this.animate(animationDurations, animationRight, 1);
    		break;
    	}
    	case LEFT:
    	{
    		this.animate(animationDurations, animationLeft, 1);
    		break;
    	}
    	case UP:
    	{
    		this.animate(animationDurations, animationUp, 1);
    		break;
    	}
    	case DOWN:
    	{
    		this.animate(animationDurations, animationDown, 1);
    		break;
    	}
    	default:
    		break;
    	}
    	super.move(direction);
	}

    @Override
    public boolean tryMove( Direction direction ) 
    {
    	if (this.m_isMoving) return false;
    	Point next = this.nextCell(direction);
    	GameObject obstacle = m_scene.findItemAtPosition( next.x, next.y );
    	if( obstacle == null ) return true;
    	obstacle.onPlayerContact();
		if( obstacle.type() == GameObject.Type.WALL ) return false;
		if( obstacle.type() == GameObject.Type.STONE )
		{
			MovingGameObject movable = ( MovingGameObject ) obstacle;
			if( movable.tryMove( direction ) )
			{
				movable.move(direction);
				return true;
			}
			else return false;
		}
		return true;
    }
}
